﻿@using Index = Flagrum.Web.Features.WorkshopMods.Editor.Index
@using Flagrum.Core.Archive
@using Flagrum.Web.Features.WorkshopMods.Data
@inject IStringLocalizer<Index> Localizer

<div class="box mt-6">
    <EditForm Model="Stats">
        <div class="p-4">
            <table class="w-full right-align-all-labels">
                <tr>
                    <td>
                        <label>@Localizer["ModTitle"]</label>
                    </td>
                    <td colspan="7" class="pl-4">
                        <Textbox Size="Textbox.Variant.Stretch" @bind-Value="Parent.Mod.WorkshopTitle"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label>@Localizer["ModDescription"]</label>
                    </td>
                    <td colspan="7" class="pl-4">
                        <Textbox Rows="3" Size="Textbox.Variant.Stretch" @bind-Value="Parent.Mod.Description"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label>@Localizer["GearMenuTitle"]</label>
                    </td>
                    <td colspan="7" class="pl-4">
                        <Textbox Size="Textbox.Variant.Stretch" @bind-Value="Parent.Mod.GameMenuTitle"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label>@Localizer["GearMenuDescription"]</label>
                    </td>
                    <td colspan="7" class="pl-4">
                        <Textbox Rows="3" Size="Textbox.Variant.Stretch" @bind-Value="Parent.Mod.GameMenuDescription"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label>@Localizer["MaxHP"]</label>
                    </td>
                    <td>
                        <NumberBox @bind-Value="Stats[WorkshopModStat.MaxHp].Value"/>
                    </td>
                    <td>
                        <label>@Localizer["MaxMP"]</label>
                    </td>
                    <td>
                        <NumberBox @bind-Value="Stats[WorkshopModStat.MaxMp].Value"/>
                    </td>
                    <td>
                        <label>@Localizer["Attack"]</label>
                    </td>
                    <td>
                        <NumberBox @bind-Value="Stats[WorkshopModStat.Attack].Value"/>
                    </td>
                    <td>
                        <label>@Localizer["BallisticRes"]</label>
                    </td>
                    <td>
                        <NumberBox Max="100" @bind-Value="Stats[WorkshopModStat.Ballistic].Value"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label>@Localizer["Strength"]</label>
                    </td>
                    <td>
                        <NumberBox @bind-Value="Stats[WorkshopModStat.Strength].Value"/>
                    </td>
                    <td>
                        <label>@Localizer["Vitality"]</label>
                    </td>
                    <td>
                        <NumberBox @bind-Value="Stats[WorkshopModStat.Vitality].Value"/>
                    </td>
                    <td>
                        <label>@Localizer["FireRes"]</label>
                    </td>
                    <td>
                        <NumberBox Max="100" @bind-Value="Stats[WorkshopModStat.Fire].Value"/>
                    </td>
                    <td>
                        <label>@Localizer["IceRes"]</label>
                    </td>
                    <td>
                        <NumberBox Max="100" @bind-Value="Stats[WorkshopModStat.Ice].Value"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label>@Localizer["Magic"]</label>
                    </td>
                    <td>
                        <NumberBox @bind-Value="Stats[WorkshopModStat.Magic].Value"/>
                    </td>
                    <td>
                        <label>@Localizer["Spirit"]</label>
                    </td>
                    <td>
                        <NumberBox @bind-Value="Stats[WorkshopModStat.Spirit].Value"/>
                    </td>
                    <td>
                        <label>@Localizer["ThunderRes"]</label>
                    </td>
                    <td>
                        <NumberBox Max="100" @bind-Value="Stats[WorkshopModStat.Thunder].Value"/>
                    </td>
                    <td>
                        <label>@Localizer["DarkRes"]</label>
                    </td>
                    <td>
                        <NumberBox Max="100" @bind-Value="Stats[WorkshopModStat.Dark].Value"/>
                    </td>
                </tr>
                <tr>
                    <td class="pt-4">
                        <label class="@(Parent.StatsTotal <= 100 ? "" : "text-error")">@Parent.StatsTotal/100</label>
                    </td>
                    <td colspan="7" class="pl-4 pt-4">
                        <div class="bg-black">
                            <div class="@(Parent.StatsTotal <= 100 ? "bg-accent1-200" : "bg-error")" style="width: @(Parent.StatsTotal <= 0 ? "0" : Parent.StatsTotal > 100 ? "100" : Parent.StatsTotal.ToString())%">&nbsp;</div>
                        </div>
                    </td>
                </tr>
            </table>
        </div>
    </EditForm>
</div>

@code
{
    [CascadingParameter]
    public Index Parent { get; set; }

    private Dictionary<WorkshopModStat, WorkshopModStats> Stats { get; set; }

    protected override void OnInitialized()
    {
        Stats = new Dictionary<WorkshopModStat, WorkshopModStats>
        {
            {
                WorkshopModStat.MaxHp, WorkshopModStats.Create(Parent.Mod.Type, Parent.Mod.Target, WorkshopModStat.MaxHp, () => Parent.Mod.MaxHp, v =>
                {
                    Parent.Mod.MaxHp = v;
                    UpdateStatCount();
                })
            },
            {
                WorkshopModStat.MaxMp, WorkshopModStats.Create(Parent.Mod.Type, Parent.Mod.Target, WorkshopModStat.MaxMp, () => Parent.Mod.MaxMp, v =>
                {
                    Parent.Mod.MaxMp = v;
                    UpdateStatCount();
                })
            },
            {
                WorkshopModStat.Strength, WorkshopModStats.Create(Parent.Mod.Type, Parent.Mod.Target, WorkshopModStat.Strength, () => Parent.Mod.Strength, v =>
                {
                    Parent.Mod.Strength = v;
                    UpdateStatCount();
                })
            },
            {
                WorkshopModStat.Vitality, WorkshopModStats.Create(Parent.Mod.Type, Parent.Mod.Target, WorkshopModStat.Vitality, () => Parent.Mod.Vitality, v =>
                {
                    Parent.Mod.Vitality = v;
                    UpdateStatCount();
                })
            },
            {
                WorkshopModStat.Magic, WorkshopModStats.Create(Parent.Mod.Type, Parent.Mod.Target, WorkshopModStat.Magic, () => Parent.Mod.Magic, v =>
                {
                    Parent.Mod.Magic = v;
                    UpdateStatCount();
                })
            },
            {
                WorkshopModStat.Spirit, WorkshopModStats.Create(Parent.Mod.Type, Parent.Mod.Target, WorkshopModStat.Spirit, () => Parent.Mod.Spirit, v =>
                {
                    Parent.Mod.Spirit = v;
                    UpdateStatCount();
                })
            },
            {
                WorkshopModStat.Fire, WorkshopModStats.Create(Parent.Mod.Type, Parent.Mod.Target, WorkshopModStat.Fire, () => Parent.Mod.Fire, v =>
                {
                    Parent.Mod.Fire = v;
                    UpdateStatCount();
                })
            },
            {
                WorkshopModStat.Ice, WorkshopModStats.Create(Parent.Mod.Type, Parent.Mod.Target, WorkshopModStat.Ice, () => Parent.Mod.Ice, v =>
                {
                    Parent.Mod.Ice = v;
                    UpdateStatCount();
                })
            },
            {
                WorkshopModStat.Thunder, WorkshopModStats.Create(Parent.Mod.Type, Parent.Mod.Target, WorkshopModStat.Thunder, () => Parent.Mod.Thunder, v =>
                {
                    Parent.Mod.Thunder = v;
                    UpdateStatCount();
                })
            },
            {
                WorkshopModStat.Dark, WorkshopModStats.Create(Parent.Mod.Type, Parent.Mod.Target, WorkshopModStat.Dark, () => Parent.Mod.Dark, v =>
                {
                    Parent.Mod.Dark = v;
                    UpdateStatCount();
                })
            },
            {
                WorkshopModStat.Ballistic, WorkshopModStats.Create(Parent.Mod.Type, Parent.Mod.Target, WorkshopModStat.Ballistic, () => Parent.Mod.Ballistic, v =>
                {
                    Parent.Mod.Ballistic = v;
                    UpdateStatCount();
                })
            }
        };

        if (Parent.Mod.Type is (int)BinmodType.Weapon or (int)BinmodType.Multi_Weapon)
        {
            Stats.Add(WorkshopModStat.Attack, WorkshopModStats.Create(Parent.Mod.Type, Parent.Mod.Target, WorkshopModStat.Attack, () => Parent.Mod.Attack, v =>
            {
                Parent.Mod.Attack = v;
                UpdateStatCount();
            }));
            Stats.Add(WorkshopModStat.Critical, WorkshopModStats.Create(Parent.Mod.Type, Parent.Mod.Target, WorkshopModStat.Critical, () => Parent.Mod.Critical, v =>
            {
                Parent.Mod.Critical = v;
                UpdateStatCount();
            }));
        }

        UpdateStatCount();
    }

    private void UpdateStatCount()
    {
        if (Stats != null)
        {
            Parent.StatsTotal = Stats.Sum(s => s.Value.Cost);
            StateHasChanged();
        }
    }
}